from os.path import basename

from xml.dom import minidom

import gtk
from widgetOrganizer import WidgetOrganizer

class Sheet(WidgetOrganizer):
    '''
    A Worksheet can hold an arbitraire amount of cells. It
    is, amongs others, responsible for completing the DOM representation.
    '''
    
    def __init__(self, name, ancestor, path=''):
        WidgetOrganizer.__init__(self, name, ancestor)
        
        self.path = path #path where it was last saved to or opened from
        self.caption = ''
        self.shortCaption = ''
        
        #create Label shown in Notebook-header
        self._createTabWidget()
    
    def _createTabWidget(self):
        '''
        Creates a widget that is shown in the tab of the notebook.
        '''
        
        caption = ''
        self.shortCaption = ''
        
        #get the captions of all other open worksheets
        captions = []
        for w in self.ancestor.children:
            captions.append(w.caption)
        
        #if there is no path to extract a name from, it gives the
        #worksheet the name 'new1', 'new2' depending on which names
        #are still free 
        if self.path=='':
            i = 0
            while True:
                caption = 'new'+str(i)
                if caption not in captions:
                    break
                i+=1
        else:
            caption = basename(self.path)
        
        #if the caption is too long a short caption is used
        if len(caption)>9:
            self.shortCaption = caption[:8] + '...'
        
        self.caption = caption
        tabWidget = gtk.HBox()
        
        #create a button to close worksheet
        exitButton = gtk.Button('<span foreground="#4f4f4f" weight="bold"> x </span>')
        exitButton.show()
        exitButton.get_children()[0].set_use_markup(True)
        exitButton.connect('clicked', lambda *args:self.ancestor.closeSheet(self))
        
        #create label
        if self.shortCaption:
            label = gtk.Label(self.shortCaption)
        else:
            label = gtk.Label(caption)
        label.show()
        
        
        tabWidget.pack_start(exitButton)
        tabWidget.pack_start(label)
        
        self.tabWidget = tabWidget
        
    def setPath(self, path):
        '''
        Changes the path variable of the worksheet and updates the tab widget
        '''
        
        self.path = path
        self._createTabWidget()
        self.ancestor.set_tab_label(self, self.tabWidget)
        
    def todom(self, **args):
        '''
        Creates a DOM representation of the whole Worksheet, inclusive
        Document Header.
        '''
        
        DOM = minidom.Document()
        rootElement = WidgetOrganizer.todom(self, *args)
        rootElement.setAttribute('title', self.caption.rsplit('.',1)[0])
        rootElement.setAttribute('xmlns', 'http://code.google.com/p/symbide/')
        DOM.appendChild(rootElement)
        DOM.normalize()
        return DOM
